package com.vehicle.service.Impl;

import com.vehicle.domains.entity.SysRoleEntity;
import com.vehicle.domains.bo.QueryRoleBo;
import com.vehicle.exception.VehicleAccessException;
import com.vehicle.mapper.SysRoleMapper;
import com.vehicle.service.SysRoleService;
import jakarta.annotation.Resource;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Override
    public void addRole(SysRoleEntity sysRoleEntity) {
        try {
            sysRoleMapper.insertRole(sysRoleEntity);
        } catch (DuplicateKeyException e) {
            throw VehicleAccessException.DATA_EXIST.newInstance("角色已存在");
        }
    }

    @Override
    public void updateRole(SysRoleEntity sysRoleEntity) {
        int i = sysRoleMapper.updateRoleById(sysRoleEntity);
        if (i == 0) {
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("修改角色未修改到数据库");
        }
    }

    @Override
    public void deleteRole(Long id) {
        int i = sysRoleMapper.deleteRoleById(id);
        if (i == 0) {
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("删除角色未删除到数据库");
        }
    }

    @Override
    public List<SysRoleEntity> selectRoleList(QueryRoleBo queryRoleBo) {
        return sysRoleMapper.selectRoleList(queryRoleBo);
    }

    @Override
    public SysRoleEntity selectByRoleId(Long id) {
        return sysRoleMapper.selectRoleById(id);
    }
}
